package com.yihao.biz.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.yihao.biz.entity.User;

//@Mapper 这里可以使用@Mapper注解，但是每个mapper都加注解比较麻烦，所以统一配置@MapperScan在扫描路径在application类中
public interface UserMapper {
    public static String TABLE = "tb_user";
	@Select("<script> SELECT * FROM "+TABLE+" WHERE id = #{id} </script>")
	User getUserById(Integer id);

	@Select("<script>" 
			 + "SELECT * "
			 + "FROM " + TABLE 
	         + " WHERE 1=1 "
	         + "<if test='username != null'>"
	         + "AND username = #{username}"
	         + "</if>"
	         + "</script>")
	public List<User> getUserList(User user);

	@Insert("INSERT INTO "+TABLE+"(username, age, ctm) values(#{username}, #{age}, now())")
	public int add(User user);

	@Update("UPDATE "+TABLE+" SET username = #{user.username} , age = #{user.age} WHERE id = #{id}")
	public int update(@Param("id") Integer id, @Param("user") User user);

	@Delete("DELETE from "+TABLE+" where id = #{id} ")
	public int delete(Integer id);
}
